package xu.zhi.qing.comment.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xu.zhi.qing.comment.entity.Comment;
import xu.zhi.qing.common.service.BaseService;
import xu.zhi.qing.user.entity.User;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Transactional
@Service
public class CommentService extends BaseService<Comment> {

    public JSONArray findByShareId(Integer shareId) {
        List<Comment> commentList = commentDao.findByShareId(shareId);
        JSONArray jList = new JSONArray();

        if (commentList != null) {
            for (Comment comment : commentList) {
                if (commentDao.get(comment.getParentId()) == null) {
                    jList.add(getFirstComment(comment));
                }
            }
        }
        return jList;
    }

    /**
     * 得到第一层的Comment的JSONObject
     *
     * @param comment
     * @return
     */
    private JSONObject getFirstComment(Comment comment) {

        JSONObject jComment = toComment(comment);

        //获取后代
        JSONArray childs = new JSONArray();
        getChilds(comment, childs);

        jComment.put("childs", childs);

        return jComment;
    }

    /**
     * 获取后代节点，保存到JSONArray中
     *
     * @param comment
     * @param childs
     */
    private void getChilds(Comment comment, JSONArray childs) {
        List<Comment> comments = commentDao.findByParentId(comment.getId());
        for (Comment c : comments) {
            childs.add(toComment(c));
            getChilds(c, childs);
        }
    }

    /**
     * 将一个Comment实体转为JSONObject
     *
     * @param comment
     * @return
     */
    private JSONObject toComment(Comment comment) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        JSONObject jComment = new JSONObject();
        jComment.put("id", comment.getId());
        jComment.put("content", comment.getContent());
        jComment.put("createTime", sdf.format(new Date(comment.getCreateTime().getTime())));
        User user = userDao.findById(comment.getUserId());
        jComment.put("userId", user.getId());
        jComment.put("username", user.getUsername());
        jComment.put("userImg", user.getImgPath());
        jComment.put("shareId", comment.getShareId());
        if (commentDao.get(comment.getParentId()) != null) {
            Comment parent = commentDao.get(comment.getParentId());
            jComment.put("pId", parent.getId());
            User pUser =  userDao.findById(parent.getUserId());
            jComment.put("pUserId", pUser.getId());
            jComment.put("pUsername", pUser.getUsername());
            jComment.put("pUserImg", pUser.getImgPath());
        }
        return jComment;
    }
}
